// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_MST_PRODUCT
// * CreateDate    : 2013-08-28 11:00:56
// * Creater       : CodeSmith
// * LastChangeDate: 2013-08-28 11:00:56
// * LastChanger   : 2013-08-28 11:00:56
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Mst
{
/// <summary>
    /// MstProduct
    /// </summary>
    public partial class MstProductBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objMstProductEntity">Entity</param>
        /// <returns>Entity</returns>
        public static MstProductEntity GetMstProduct(MstProductEntity objMstProductEntity)
        {
            return DataAccess.SelectSingle<MstProductEntity>(objMstProductEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objMstProductEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertMstProduct(MstProductEntity objMstProductEntity)
        {
            return DataAccess.Insert(objMstProductEntity);
        }

        public static bool InsertMstProduct(List<MstProductEntity> objMstProductEntityList)
        {
            return DataAccess.Insert(objMstProductEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objMstProductEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateMstProduct(MstProductEntity objMstProductEntity)
        {
            return DataAccess.Update(objMstProductEntity);
        }

        public static bool UpdateMstProduct(List<MstProductEntity> objMstProductEntityList)
        {
            return DataAccess.Update<MstProductEntity>(objMstProductEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objMstProductEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteMstProduct(MstProductEntity objMstProductEntity)
        {
            return DataAccess.Delete(objMstProductEntity);
        }

        public static bool DeleteMstProduct(List<MstProductEntity> objMstProductEntityList)
        {
            return DataAccess.Delete<MstProductEntity>(objMstProductEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetMstProductAll()
        {
            return DataAccess.SelectAllDataSet<MstProductEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<MstProductEntity> GetMstProductAllList()
        {
            return DataAccess.SelectAll<MstProductEntity>();
        }
        #endregion

        public static DataSet GetAllDsByCondition(int pageIndex, int pageCount, string condition)
        {
            string sql = "";
            if (string.IsNullOrEmpty(condition))
            {
                string sqlSource = @"select top {0} *,(select count(1) from MST_PRODUCT) as cnt
from MST_PRODUCT
where product_id not in (
    select top ({1}*({2}-1)) product_id from MST_PRODUCT )";
                sqlSource = string.Format(sqlSource, pageCount, pageCount, pageIndex);
                sql = @"SELECT A.*,C.PRODUCT_TYPE_CODE AS MAIN_TYPE_CODE,C.PRODUCT_TYPE_NAME AS MAIN_TYPE_NAME,
B.PRODUCT_TYPE_CODE AS MID_TYPE_CODE,B.PRODUCT_TYPE_NAME AS MID_TYPE_NAME 
from ({0}) A 
left join MST_PRODUCT_TYPE B on A.PRODUCT_TYPE_ID=B.PRODUCT_TYPE_ID
left join MST_PRODUCT_TYPE C on B.PARENT_TYPE_ID=C.PRODUCT_TYPE_ID
WHERE 1=1 and A.DELETE_FLAG=0 ";
                sql = string.Format(sql, sqlSource);
            }
            else
            {
                sql = @"select top {0} *,(select count(1) from ({3}) t) as cnt
from ({3}) t
where t.product_id not in (
    select top ({1}*({2}-1)) product_id from ({3}) t)";
                string sqlSource = @"SELECT A.*,C.PRODUCT_TYPE_CODE AS MAIN_TYPE_CODE,
C.PRODUCT_TYPE_NAME AS MAIN_TYPE_NAME,
B.PRODUCT_TYPE_CODE AS MID_TYPE_CODE,B.PRODUCT_TYPE_NAME AS MID_TYPE_NAME 
from MST_PRODUCT A 
left join MST_PRODUCT_TYPE B on A.PRODUCT_TYPE_ID=B.PRODUCT_TYPE_ID
left join MST_PRODUCT_TYPE C on B.PARENT_TYPE_ID=C.PRODUCT_TYPE_ID
WHERE 1=1 and A.DELETE_FLAG=0 ";
                sqlSource += condition;
                sql = string.Format(sql, pageCount, pageCount, pageIndex, sqlSource);
            }
            return DataAccess.SelectDataSet(sql);
        }

        public static DataSet GetMaterialExAllDsByCondition(int pageIndex, int pageCount, string condition)
        {
            string sql = "";
            if (string.IsNullOrEmpty(condition))
            {
                string sqlSource = @"select top {0} *,(select count(1) from MST_MATERIAL_EX) as cnt
from MST_MATERIAL_EX
where material_id not in (
    select top ({1}*({2}-1)) material_id from MST_MATERIAL_EX )";
                sqlSource = string.Format(sqlSource, pageCount, pageCount, pageIndex);
                sql = @"SELECT mex.*,prd.PRODUCT_NAME,prd.PRODUCT_CODE,prd.TYPE_NAME,
prd.STD_NAME,prd.MATERIAL,prd.UNIT,prd.MINOR_UNIT,prd.SUGGEST_PRICE
from ({0}) mex
inner join MST_PRODUCT prd on mex.MATERIAL_ID=prd.PRODUCT_ID
WHERE 1=1 and prd.DELETE_FLAG=0 ";
                sql = string.Format(sql, sqlSource);
            }
            else
            {
                sql = @"select top {0} *,(select count(1) from ({3}) t) as cnt
from ({3}) t
where t.material_id not in (
    select top ({1}*({2}-1)) material_id from ({3}) t)";
                string sqlSource = @"SELECT  mex.*,prd.PRODUCT_NAME,prd.PRODUCT_CODE,prd.TYPE_NAME,
prd.STD_NAME,prd.MATERIAL,prd.UNIT,prd.MINOR_UNIT,prd.SUGGEST_PRICE
from MST_MATERIAL_EX mex 
inner join MST_PRODUCT prd on mex.MATERIAL_ID=prd.PRODUCT_ID
WHERE 1=1 and prd.DELETE_FLAG=0 ";
                sqlSource += condition;
                sql = string.Format(sql, pageCount, pageCount, pageIndex, sqlSource);
            }
            return DataAccess.SelectDataSet(sql);
        }

	}
	
	
}	

